package com.example.demo.service;

import com.example.demo.constants.ContractConstants;
import com.example.demo.model.bo.DigitalRightsGetWorkInputBO;
import com.example.demo.model.bo.DigitalRightsSetWorkInputBO;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.transaction.manager.AssembleTransactionProcessor;
import org.fisco.bcos.sdk.transaction.manager.TransactionProcessorFactory;
import org.fisco.bcos.sdk.transaction.model.dto.TransactionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
@NoArgsConstructor
@Data
public class DigitalRightsService {
  @Value("${contract.digitalRightsAddress}")
  private String address;

  @Autowired
  private Client client;

  AssembleTransactionProcessor txProcessor;

  @PostConstruct
  public void init() throws Exception {
    this.txProcessor = TransactionProcessorFactory.createAssembleTransactionProcessor(this.client, this.client.getCryptoSuite().getCryptoKeyPair());
  }

  public TransactionResponse setWork(DigitalRightsSetWorkInputBO input) throws Exception {
    return this.txProcessor.sendTransactionAndGetResponse(this.address, ContractConstants.DigitalRightsAbi, "setWork", input.toArgs());
  }

  public TransactionResponse getWork(DigitalRightsGetWorkInputBO input) throws Exception {
    return this.txProcessor.sendTransactionAndGetResponse(this.address, ContractConstants.DigitalRightsAbi, "getWork", input.toArgs());
  }
}
